package grape.attend.rest.overtime.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.attend.rest.overtime.form.OvertimeWorkListForm;
import grape.attend.service.overtime.query.OvertimeWorkListQuery;
import grape.attend.service.overtime.query.OvertimeWorkWithNullCondition;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.attend.rest.overtime.form.OvertimeWorkCreateForm;
import grape.attend.rest.overtime.form.OvertimeWorkUpdateForm;
import grape.attend.rest.overtime.form.OvertimeWorkListPageForm;
import grape.attend.rest.overtime.vo.OvertimeWorkVo;
import grape.attend.rest.overtime.mapper.OvertimeWorkWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.attend.service.overtime.po.OvertimeWork;
import grape.attend.service.overtime.api.IOvertimeWorkService;
import java.util.List;
/**
 * <p>
 * 加班工作日设置表 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-05-09
 */
@RestController
@RequestMapping("/attend/overtimework")
@Api(tags = "加班工作日设置相关接口")
public class OvertimeWorkController extends BaseController<OvertimeWorkVo, OvertimeWork> {

    @Autowired
    private OvertimeWorkWebMapper currentWebMapper;
    @Autowired
    private IOvertimeWorkService currentService;



     @ApiOperation("添加加班工作日设置")
     @PreAuthorize("hasAuthority('overtimework:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public OvertimeWorkVo create(@RequestBody @Valid OvertimeWorkCreateForm cf) {
         OvertimeWork po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询加班工作日设置")
     @PreAuthorize("hasAuthority('overtimework:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public OvertimeWorkVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除加班工作日设置")
     @PreAuthorize("hasAuthority('overtimework:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新加班工作日设置")
     @PreAuthorize("hasAuthority('overtimework:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public OvertimeWorkVo update(@PathVariable String id,@RequestBody @Valid OvertimeWorkUpdateForm uf) {
         OvertimeWork po = currentWebMapper.formToPo(uf);
         po.setId(id);
         if(!po.getIsAllowOvertime()){
             OvertimeWorkWithNullCondition overtimeWorkWithNullCondition = new OvertimeWorkWithNullCondition();
             overtimeWorkWithNullCondition.setIsApprove(true);
             overtimeWorkWithNullCondition.setIsDeductRestTime(true);
             overtimeWorkWithNullCondition.setRestTime(true);
             overtimeWorkWithNullCondition.setPo(po);
             return super.update(overtimeWorkWithNullCondition);
         }
         if(!po.getIsDeductRestTime()){
             OvertimeWorkWithNullCondition overtimeWorkWithNullCondition = new OvertimeWorkWithNullCondition();
             overtimeWorkWithNullCondition.setRestTime(true);
             overtimeWorkWithNullCondition.setPo(po);
             return super.update(overtimeWorkWithNullCondition);
         }
         return super.update(po);
     }
    @ApiOperation("不分页查询加班工作日设置")
    @PreAuthorize("hasAuthority('overtimework:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<OvertimeWorkVo> list(OvertimeWorkListForm listForm) {
        OvertimeWork po = currentWebMapper.formToPo(listForm);
        OvertimeWorkListQuery overtimeWorkListQuery = new OvertimeWorkListQuery();
        overtimeWorkListQuery.setPoQuery(po);
        return super.list(overtimeWorkListQuery);
    }
    @ApiOperation("分页查询加班工作日设置")
    @PreAuthorize("hasAuthority('overtimework:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<OvertimeWorkVo> listPage(OvertimeWorkListPageForm listPageForm) {
         OvertimeWork po = currentWebMapper.formToPo(listPageForm);
        OvertimeWorkListQuery overtimeWorkListQuery = new OvertimeWorkListQuery();
        overtimeWorkListQuery.setPoQuery(po);
        return super.listPage(overtimeWorkListQuery,listPageForm);
     }

}
